package mth.masterimp;

import java.io.FileWriter;
import java.io.IOException;

public class Question {
	/*
	 * Diese Klasse bildet das Stellen der Fragen ab und speichert bereits gestellte Fragen
	 * zu den Datenelementen.
	 */
	
	private int[][] queryList;
	private static int queries;
	private DataRepresentator dr;
	
	Question(DataRepresentator d) {
		dr=d;
		queryList = new int[dr.getDistanceMatrix().length][dr.getDistanceMatrix().length];
	}
	
	public boolean ask(int a, int b) {
		//well...this is an interesting point:
		//if a question is asked more than one time
		//it will get an extra count...
		//as a backup for getting the unique amount of questions use 
		//getAmountOfAllQueries()
//		if (queryList[a][b]==0)
			incrementQueries();
		double[] a_element=null;
		double[] b_element=null;
		a_element=Tools.extractLongDoubleArrayFromDataRow(dr.getValueMatrix().getRow(a));
		b_element=Tools.extractLongDoubleArrayFromDataRow(dr.getValueMatrix().getRow(b));
		int label = dr.getValueMatrix().getColumnIndex("label");
//		System.out.println("Frage "+queries+" nach: "+a+" in Cluster: "+a_element[label]+" und "+b+" in Cluster: "+b_element[label]);
		queryList[a][b]=(compare(a_element[label],b_element[label]))?1:-1;
//		System.out.println("erfolgreich:"+getAmountOfPositiveQueries()+", nicht erfolgreich:"+getAmountOfNegativeQueries()+"\n");
		return compare(a_element[label],b_element[label]);
	}
	
	private boolean compare(double x, double y) {
		return (x==y);
	}
	
	public int getAmountOfAllQueries() {
		return getAmountOfNegativeQueries()+getAmountOfPositiveQueries();
	}
	
	public int getAmountOfNegativeQueries() {
		int collector=0;
		for (int a=0; a<queryList.length; a++) {
			for (int b=0; b<queryList.length; b++) {
				if (queryList[a][b]<0)
					collector++;
			}
		}
		return collector;
	}
	
	public int getAmountOfPositiveQueries() {
		int collector=0;
		for (int a=0; a<queryList.length; a++) {
			for (int b=0; b<queryList.length; b++) {
				if (queryList[a][b]>0)
					collector++;
			}
		}
		return collector;
	}
	
	public int getQueryState(int x, int y) {
		if(queryList[x][y]<0) 
			return -1;
		if (queryList[x][y]>0)
			return 1;
		return 0;
	}
	
	private void incrementQueries() {
		queries++;
	}
	
	public boolean isNewQuestion(int a, int b) {
		return (queryList[a][b]==0 && a!=b && queryList[b][a]==0);
	}
	
	public void print(String file) {
		String outputString="";
		
		outputString+="Questions: "+queries+" successful: "+ getAmountOfPositiveQueries() +
				 " not successful: "+getAmountOfNegativeQueries()+"\n";
		
		try {
			FileWriter fw = new  FileWriter(file+"_Queries_prop", true);
			fw.write(outputString);
			fw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
//		outputString="";
//		for (int a=0; a<queryList.length; a++) {
//			for (int b=0; b<queryList[a].length; b++) {
//				if (b==queryList[a].length-1) {
//					outputString+=queryList[a][b]+"\n";
//				} else {
//					outputString+=queryList[a][b]+",";
//				}
//			}
//		}
//		
//		try {
//			FileWriter fw = new  FileWriter(file+"_Queries_matix", false);
//			fw.write(outputString);
//			fw.close();
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
	}
}
